//
//  CmtSecMoreLikeViewController.m
//  Products_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtSecMoreLikeViewController.h"
#define kUrlString @"http://api.izhangchu.com/"
#import "RequestManager.h"
#import "CmtAttenMLTableViewCell.h"
#import "CmtSecAttenRelates.h"
#import "CmtShenQuViewController.h"

@interface CmtSecMoreLikeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *relatesArray;
@property(nonatomic,strong)UIView *topView;

@end

@implementation CmtSecMoreLikeViewController

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    _topView.hidden = YES;
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    _topView.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 70)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.view addSubview:_topView];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(10, 30, 30, 30);
    [button setTitle:@"<—" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(barButtonDismiss:) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:button];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 150)/2, 30, 150, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"TA们都点了赞";
    [_topView addSubview:label];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, kWidth, kHeight - 50) style:(UITableViewStylePlain)];
//    self.tableView.backgroundColor = [UIColor cyanColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    self.relatesArray = [[NSMutableArray alloc]initWithCapacity:10];
    [self requestDataWithPraise];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CmtAttenMLTableViewCell" bundle:nil] forCellReuseIdentifier:@"MLCell"];
    
    // Do any additional setup after loading the view.
}

-(void)requestDataWithPraise{
    
   
    NSDictionary *parDic = @{@"post_id":self.post_id,@"page":@"1",@"methodName":@"DianzanList",@"size":@"20",@"version":@"4.40&"};

    [RequestManager requestWithUrl:kUrlString requestType:(RequestTypePOST) parDic:parDic finish:^(NSData *data) {
        NSDictionary *jsdic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
//        NSLog(@"%@",jsdic);
        
        NSArray *dataArray = jsdic[@"data"][@"data"];
        for (NSDictionary *dic in dataArray) {
            CmtSecAttenRelates *model = [[CmtSecAttenRelates alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.relatesArray addObject:model];
        }
        [self.tableView reloadData];
        
//        NSLog(@"%lu",(unsigned long)self.relatesArray.count);
        
    } error:nil];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.relatesArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CmtAttenMLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MLCell" forIndexPath:indexPath];
    
    CmtSecAttenRelates *relates = self.relatesArray[indexPath.row];
    [cell.head_img sd_setImageWithURL:[NSURL URLWithString:relates.head_img]];
    cell.nick.text = [NSString stringWithFormat:@"%@",relates.nick];
    cell.create_time.text = [NSString stringWithFormat:@"%@",relates.create_time_cn];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    cell.head_img.tag = 100 + indexPath.row;
    cell.head_img.userInteractionEnabled = YES;
    [cell.head_img addGestureRecognizer:tap];
    
    
    return cell;
}

-(void)tapAction:(UITapGestureRecognizer *)tap{

    NSInteger index = tap.view.tag - 100;
    
    CmtSecAttenRelates *model = self.relatesArray[index];
    
    CmtShenQuViewController *CmtSQVc = [[CmtShenQuViewController alloc]init];
    CmtSQVc.user_id = model.user_id;
    
    [self.navigationController pushViewController:CmtSQVc animated:YES];
    

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}


//返回的方法
-(void)barButtonDismiss:(UIBarButtonItem *)barButton{

    [self.navigationController popViewControllerAnimated:YES];
}
//隐藏选项栏
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
